
import { LIVE_OPEN_PLATFORM_DM_TYPE, 
         LIVE_OPEN_PLATFORM_GUARD_TYPE, 
         LIVE_OPEN_PLATFORM_SEND_GIFT_TYPE, 
         LIVE_OPEN_PLATFORM_SUPER_CHAT_DEL_TYPE, 
         LIVE_OPEN_PLATFORM_SUPER_CHAT_TYPE, 
         SocketCmdEnum, 
         SocketMsgType 
        } from "@/types/types";
import { insertDM } from "./logboard";
import PIXIAppHandler from "./PIXIAppHandler";
import CONFIG from '@/config';
import { convert, isNum } from "@/tool/stringUtils";

//根据cmd判断类型
export function operator(e: SocketMsgType){
    if(e.cmd == SocketCmdEnum.LIVE_OPEN_PLATFORM_DM){
        dm(e)
    }
    else if(e.cmd == SocketCmdEnum.LIVE_OPEN_PLATFORM_GUARD){
        guard(e)
    }
    else if(e.cmd == SocketCmdEnum.LIVE_OPEN_PLATFORM_SEND_GIFT){
        send_gift(e)
    }
    else if(e.cmd == SocketCmdEnum.LIVE_OPEN_PLATFORM_SUPER_CHAT){
        super_chat(e)
    }
    else if(e.cmd == SocketCmdEnum.LIVE_OPEN_PLATFORM_SUPER_CHAT_DEL){
        super_hat_del(e)
    }
}

function dm(e: SocketMsgType<LIVE_OPEN_PLATFORM_DM_TYPE>){
    draw(e)
}
function guard(e: SocketMsgType<LIVE_OPEN_PLATFORM_GUARD_TYPE>){
    console.log("[operator] 大航海 => 感谢 " 
                + e.data.user_info.uname 
                + " 开通的大航海"
    );
}
function send_gift(e: SocketMsgType<LIVE_OPEN_PLATFORM_SEND_GIFT_TYPE>){
    console.log("[operator] 送礼物 => 感谢 " 
                + e.data.uname 
                + " 送出的 " 
                + e.data.gift_num
                + "个"
                + e.data.gift_name
    );
}
function super_chat(e: SocketMsgType<LIVE_OPEN_PLATFORM_SUPER_CHAT_TYPE>){
    console.log("[operator] 付费留言 => " 
                + e.data.uname 
                + " 支付 " 
                + e.data.rmb 
                + " 元说: " 
                + e.data.message
    );
}
function super_hat_del(e: SocketMsgType<LIVE_OPEN_PLATFORM_SUPER_CHAT_DEL_TYPE>){
    console.log("[operator] 付费留言下线 => " 
                + e.data.message_ids
    );
}


function draw(e: SocketMsgType) {
    let handler: any = PIXIAppHandler.getInstance()
    let msg = e.data.uname 
            + " 说: " 
            + e.data.msg

    console.log("[operator] 弹幕 => " + msg)
    insertDM(msg)


    const tmp = split(e.data.msg)
    console.log(...tmp.args)
    handler[tmp.type](...tmp.args)

}

function split(msg: string) {
    let color, type: string = "", messageLength
    let args: number[] = []
    //operations
    let ops = msg.split(/[,，（）()\s]+/)

    //点
    if(ops.length == 3){
        type = "drawPoint"
    }
    //圆
    else if(ops.length == 4){
        type = "drawCircle"
    }
    //线
    else if(ops.length == 5){
        type = "drawRectangle"
    }
    else return {type: "none", args:[]}

    args = JSON.parse(JSON.stringify(ops))
    args[args.length - 1] = findColor(args[args.length - 1])

    // if (ops.length != 0)
    //   type = ops[0]
    
    // args = JSON.parse(JSON.stringify(ops))
    // args.shift()

    // args[0] = parseInt("0x" + args[0])
    // for(let i = 1; i < args.length; i++){
    //     args[i] = parseInt("" + args[i])
    // }


    return {type, args}
}

function findMethod(type: any): string {
    let map = convert(CONFIG.TYPE_MAP)
    return map[type.toLowerCase()]
}

function findColor(color: any): number {
    console.log(color)
    if(isNum(color)) return parseInt("0x" + color)
    let map = CONFIG.COLOR_MAP
    return parseInt(map[color].toLowerCase())
}